﻿CREATE PROCEDURE [Admin].[sp_z4s_decrypt_data]
	@encryptedValue varbinary(MAX),
	@idValue uniqueidentifier,
	@keyPassword nvarchar(255),
	@decryptedValue nvarchar(MAX) OUTPUT
AS
BEGIN
	-- Open symmetric key
	Declare @openKey nvarchar(MAX)
	set @openKey='
	Open Symmetric Key Zen4SyncSymmetricKey
	decryption By password ='''+@keyPassword+''''

	exec [dbo].[sp_executesql] @openKey
 
	--Encryption of the decrypted value
	select @decryptedValue = convert(nvarchar(MAX),DecryptByKey( @encryptedValue, 1, CONVERT(nvarchar(36), @idValue)))

	Close Symmetric Key Zen4SyncSymmetricKey

	-- Return the result of the function
	RETURN 0

END